package com.kinglone.back.user.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.kinglone.back.role.model.Role;
import com.kinglone.back.role.service.RoleService;
import com.kinglone.back.user.model.User;
import com.kinglone.back.user.service.UserService;
import com.kinglone.common.utils.CryptoUtil;
import com.kinglone.common.utils.JSONMessageView;
import com.kinglone.common.utils.StringUtil;

@Controller
@RequestMapping("/back/user")
public class UserController {

	@Autowired
	private UserService userService;
	@Autowired
	private RoleService roleService;
	
	/**
	 * 登陆 
	 * @param request
	 * @param user
	 * @return
	 */
	@RequestMapping("/login.htm")
	public String login(HttpServletRequest request,User user){
			Subject subject = SecurityUtils.getSubject();
			UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(),CryptoUtil.md5(user.getPassword(), "kinglone"));
			System.out.println("----token:"+token);
			try {
				subject.login(token);//登陆验证
				return "redirect:/back/index.jsp";
			} catch (Exception e) {
				e.printStackTrace();
				request.setAttribute("errorMessage", "用户名或密码错误！");
				return "login";
			}			
	}
	
	/**
	 * 跳转到用户列表
	 * @param request
	 * @return
	 */
	@RequestMapping("/user-list.htm")
	public ModelAndView userList(HttpServletRequest request){
		ModelAndView mv = new ModelAndView("back/user/user-list");
		List<User> userList = userService.getAllUsers();
		mv.addObject("userList", userList);
		return mv;
	}
	/**
	 * 跳转修改用户页面
	 * @param request
	 * @param model
	 * @return
	 */
	/*@RequestMapping("/showUpdateUser.htm")
	public ModelAndView showUpdateUser(HttpServletRequest request,Model model){
		ModelAndView mv = new ModelAndView("back/user-list");
	}*/
	
	/**
	 * 添加用户
	 * @param user
	 * @param request
	 * @return
	 */
	@RequestMapping("/addUser.htm")
	public JSONMessageView addUser(User user,HttpServletRequest request){
		
		JSONMessageView msg = new JSONMessageView();
		if(StringUtil.isEmpty(user.getUsername())){
			msg =new JSONMessageView(-1,"请输入用户名!",null);
			return msg;
		}
		if(StringUtil.isEmpty(user.getPassword())){
			msg =new JSONMessageView(-1,"请输入密码!",null);
			return msg;
		}
		user.setPassword(CryptoUtil.md5(user.getPassword(), "kinglone"));
	try {
		userService.addUser(user);
		msg = new JSONMessageView(0,"用户添加成功!",null);
		return msg;
	} catch (Exception e) {
		e.printStackTrace();
		msg =new JSONMessageView(-1,"用户添加失败!",null);
		return msg;
	}
  }
	/**
	 * 删除用户
	 * @return
	 */
	@RequestMapping("/delUser.htm")
	public JSONMessageView delUser(@RequestParam(value="userId") String userId){
		JSONMessageView msg = new JSONMessageView();
		try {
			userService.delUserByUserId(userId);
			msg = new JSONMessageView(0,"删除成功!",null);
			return msg;		
		} catch (Exception e) {
			e.printStackTrace();
			msg = new JSONMessageView(-1,"删除失败!",null);
			return msg;
		}
	}
	
	/**
	 * 跳转到角色授权页面
	 * @param userId
	 * @param request
	 * @return
	 */
	@RequestMapping("/user-add-role.htm")
	public ModelAndView userAddRole(HttpServletRequest request){
		ModelAndView mv = new ModelAndView("back/user/user-add-role");
		List<Role> roleList = roleService.getAllRoles();
		mv.addObject("roleList", roleList);
		return mv;
	}
}
